package cn.highedu;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Set;


@SpringBootApplication
public class Unit04Application {

	public static void main(String[] args) {
		SpringApplication.run(Unit04Application.class, args);
	}

	@Bean
	public CommandLineRunner commandLineRunner(DataSource dataSource) {
//		CommandLineRunner runner = new CommandLineRunner() {
//			@Override
//			public void run(String... args) throws Exception {
//				System.out.println("Hello World!");
//				System.out.println("args: " + Arrays.toString(args));
//			}
//		};
//		return runner;
		return args -> {
			System.out.println("Hello World!");
			System.out.println("args: " + Arrays.toString(args)); // 原始参数数组
			System.out.println("dataSource: " + dataSource.getClass().getName());
		};
	}

	@Bean
	public ApplicationRunner applicationRunner() {
//		ApplicationRunner runner = new ApplicationRunner() {
//			@Override
//			public void run(ApplicationArguments args) throws Exception {
//				// ApplicationArguments 解析了命令行参数为对象
//				System.out.println("Hello World!");
//				//遍历所有参数
//				Set<String> optionNames = args.getOptionNames();
//				for (String optionName : optionNames) {
//					System.out.println(optionName + "=" + args.getOptionValues(optionName));
//				}
//			}
//		};
//		return runner;
		return args -> {
			// ApplicationArguments 解析了命令行参数为对象
			System.out.println("Hello World!");
			//遍历所有参数
			Set<String> optionNames = args.getOptionNames();
			for (String optionName : optionNames) {
				System.out.println(optionName + "=" + args.getOptionValues(optionName));
			}
		};
	}
}
